package cn.yunhe.controllor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import cn.yunhe.entity.Goods;
import cn.yunhe.service.PageService;
	
public class UploadController extends ActionSupport {
	@Autowired
	private PageService ps;
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String gname;
	private String gdespriction;
	private BigDecimal gprice;
	private BigDecimal gnum;
	private File gimage;
	private String gimageFileName;
	private String gimageContentType;
	//更新商品操作
	public String toEditGoods(){
		String path = "D:\\nginx-1.11.6\\html\\curd\\";
		String fileid = String.valueOf(new Date().getTime())+gimageFileName;
		System.out.println("path:"+path);
		String filename = path+"/"+fileid;
		FileInputStream in = null;
		FileOutputStream out = null;
		if(gimage!=null){
			try {
				in = new FileInputStream(gimage);
				out = new FileOutputStream(filename);
				byte[] b = new byte[1024];
				int len = 0;
				while((len=in.read(b)) > 0){
					out.write(b,0,len);
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Goods goods = new Goods(id,gname,gdespriction,gprice,gnum, gimageFileName,fileid);
			int result = ps.modifyGoods1(goods);
		}else{
			Goods goods = new Goods(id,gname,gdespriction,gprice,gnum);
			int result = ps.modifyGoods2(goods);
		}
		
		
		return "success";
	}
	//增加商品操作
	public String toAddGoods(){
		String path = "D:\\nginx-1.11.6\\html\\curd\\";
		String fileid = String.valueOf(new Date().getTime())+gimageFileName;
		System.out.println("path:"+path);
		String filename = path+"/"+fileid;
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(gimage);
			out = new FileOutputStream(filename);
			byte[] b = new byte[1024];
			int len = 0;
			while((len=in.read(b)) > 0){
				out.write(b,0,len);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Goods goods = new Goods(gname,gdespriction,gprice,gnum, gimageFileName,fileid);
		int result = ps.addGoods(goods);
		System.out.println(goods.toString());
		return "success";
	}

	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getGname() {
		return gname;
	}

	public void setGname(String gname) {
		this.gname = gname;
	}

	public String getGdespriction() {
		return gdespriction;
	}

	public void setGdespriction(String gdespriction) {
		this.gdespriction = gdespriction;
	}

	public BigDecimal getGprice() {
		return gprice;
	}

	public void setGprice(BigDecimal gprice) {
		this.gprice = gprice;
	}

	public BigDecimal getGnum() {
		return gnum;
	}

	public void setGnum(BigDecimal gnum) {
		this.gnum = gnum;
	}

	public File getGimage() {
		return gimage;
	}

	public void setGimage(File gimage) {
		this.gimage = gimage;
	}

	public String getGimageFileName() {
		return gimageFileName;
	}

	public void setGimageFileName(String gimageFileName) {
		this.gimageFileName = gimageFileName;
	}

	public String getGimageContentType() {
		return gimageContentType;
	}

	public void setGimageContentType(String gimageContentType) {
		this.gimageContentType = gimageContentType;
	}
	
}
